package com.lurencun.android.topicbank.res;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lurencun.android.support.widget.CommonAdapter;
import com.lurencun.android.topicbank.R;
import com.lurencun.android.topicbank.presist.Paper;
import com.mappn.sdk.pay.GfanPay;
import com.mappn.sdk.pay.GfanPayCallback;
import com.mappn.sdk.pay.model.Order;
import com.mappn.sdk.uc.User;

public class PaperAdapter extends CommonAdapter<Paper> {

	private Context mContext;

	final Handler handler;
	
	public PaperAdapter(Context context, Handler paperHandler) {
		super(context);
		mContext = context;
		handler = paperHandler;
	}

	protected View createView(LayoutInflater inflater, final Paper data,
			int position, View convertView, ViewGroup parent) {
		RelativeLayout cell = (RelativeLayout) inflater.inflate(
				R.layout.category_cell, null);
		TextView title = (TextView) cell.findViewById(R.id.paper_title);
		TextView desc = (TextView) cell.findViewById(R.id.paper_describe);
		TextView price = (TextView) cell.findViewById(R.id.paper_price);
		title.setText(data.getTitle());
		desc.setText(data.getTypeTitle());

		LinearLayout back_button = (LinearLayout) cell
				.findViewById(R.id.back_button);
		Button buyBtn = (Button) cell.findViewById(R.id.buyBtn);
		Button downBtn = (Button) cell.findViewById(R.id.downBtn);
		
		buyBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(
						mContext,"buy " + data.getTitle(),
						Toast.LENGTH_SHORT).show();
				
				buyPaper(data);
			}
		});
		
		downBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Message msg = Message.obtain(handler, 0, data.getPaperId());
				handler.sendMessage(msg);
			}
		});
		
		// 如果已经购买
		if (data.isPurchased()) {
			// 没有topic
			if (data.getTopics() == null || data.getTopics().size() == 0) {
				back_button.setVisibility(View.VISIBLE);
				buyBtn.setVisibility(View.GONE);
				downBtn.setVisibility(View.VISIBLE);
			} else {
				back_button.setVisibility(View.GONE);
				buyBtn.setVisibility(View.GONE);
				downBtn.setVisibility(View.GONE);
			}
		} else {
			back_button.setVisibility(View.VISIBLE);
			buyBtn.setVisibility(View.VISIBLE);
			downBtn.setVisibility(View.GONE);
		}

		float paperPrice = data.getPrice();
		if (paperPrice == 0) {
			price.setText("free");
			price.setTextColor(mContext.getResources().getColor(
					R.color.darkgreen));
		} else {
			price.setText("￥" + Float.toString(paperPrice));
		}
		return cell;
	}
	
	private void buyPaper(Paper data) {
		String orderId = "order" + System.currentTimeMillis();
		Order order = new Order("product name", "product description", 50, orderId);
		GfanPay.getInstance(mContext.getApplicationContext()).pay(order,
				new GfanPayCallback() {

					@Override
					public void onSuccess(User user, Order order) {
						Toast.makeText(
								mContext.getApplicationContext(),
								"pay success user：" + user.getUserName() + " pay point："
										+ order.getMoney(), Toast.LENGTH_SHORT)
								.show();
					}

					@Override
					public void onError(User user) {
						if (user != null) {
							Toast.makeText(mContext.getApplicationContext(),
									"pay failure, user：" + user.getUserName(),
									Toast.LENGTH_SHORT).show();
						} else {
							Toast.makeText(mContext.getApplicationContext(), "failed login",
									Toast.LENGTH_SHORT).show();
						}
					}
				});
		
	}
	
}
